package com.example.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.enums.BusinessTypeEnum;
import com.example.common.enums.OperatorTypeEnum;
import com.example.common.enums.StatusTypeEnum;
import com.example.manager.service.SysOperLogService;
import com.example.manager.mapper.SysOperLogMapper;
import com.example.model.dto.system.SysOperLogDto;
import com.example.model.entity.system.SysOperLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements SysOperLogService {

    @Autowired
    private SysOperLogMapper sysOperLogMapper;

    @Override
    public Page<SysOperLog> findAllSysOperLog(SysOperLogDto sysOperLogDto) {
        Page<SysOperLog> page = new Page<>(sysOperLogDto.getPageNum(), sysOperLogDto.getPageSize());

        LambdaQueryWrapper<SysOperLog> sysOperLogLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if(StringUtils.isNotEmpty(sysOperLogDto.getTitle())){
            sysOperLogLambdaQueryWrapper.like(SysOperLog::getTitle, sysOperLogDto.getTitle());
        }
        if(StringUtils.isNotEmpty(sysOperLogDto.getOperName())){
            sysOperLogLambdaQueryWrapper.like(SysOperLog::getOperName, sysOperLogDto.getOperName());
        }
        if (StringUtils.isNotEmpty(sysOperLogDto.getCreateTimeBegin())) {
            sysOperLogLambdaQueryWrapper.ge(SysOperLog::getCreateTime, sysOperLogDto.getCreateTimeBegin())
                    .le(SysOperLog::getCreateTime, sysOperLogDto.getCreateTimeEnd());
        }


        Page<SysOperLog> sysOperLogPage = sysOperLogMapper.selectPage(page, sysOperLogLambdaQueryWrapper);

        //businessType和operatorType 替换枚举值
        sysOperLogPage.getRecords().forEach(sysOperLog -> {
            sysOperLog.setBusinessTypeText(BusinessTypeEnum.getValue(sysOperLog.getBusinessType()));
            sysOperLog.setOperatorTypeText(OperatorTypeEnum.getValue(sysOperLog.getOperatorType()));
            sysOperLog.setStatusText(StatusTypeEnum.getValue(sysOperLog.getStatus()));
        });
        return sysOperLogPage;
    }
}
